<?php
define('API_KEY',      '77as2hpxylyffo'                                             );
define('API_SECRET',   'rhDBlVn9TeIqouu1'                                           );

define('REDIRECT_URI', 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']);
define('SCOPE',        'r_basicprofile r_emailaddress r_network'                              );

session_name('linkedin');
session_start();
 
if (isset($_GET['error'])) {

    print $_GET['error'] . ': ' . $_GET['error_description'];
    exit;
} elseif (isset($_GET['code'])) {
    if ($_SESSION['state'] == $_GET['state']) {
        getAccessToken();
    } else {
        exit;
    }
} else { 
    if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
        $_SESSION = array();
    }
    if (empty($_SESSION['access_token'])) {
        getAuthorizationCode();
    }
}

function getInfos(){
$user = fetch('GET', '/v1/people/~:(firstName,lastName)');
$connections = fetch('GET', '/v1/people/~/connections:(positions,first-name,last-name)');

$json = array();
$i = 0;
foreach($connections->values as $value)
{
    $positions = $value->positions;
       foreach($positions as $comp) {
                $res = $comp[0];
                if ($res != null)
                {
                    $res = $res->company;
                }
                if ($res != null)
                {
                    $json[$i] = array (
                        $value->firstName,
                        $res->name
                    );
                }
        }
    $i++;
}

return (json_encode($json));
}


 
function getAuthorizationCode() {
    $params = array(
        'response_type' => 'code',
        'client_id' => API_KEY,
        'scope' => SCOPE,
        'state' => uniqid('', true),
        'redirect_uri' => REDIRECT_URI,
    );
 
    $url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);
     
    $_SESSION['state'] = $params['state'];
 
    header("Location: $url");
    exit;
}
     
function getAccessToken() {
    $params = array(
        'grant_type' => 'authorization_code',
        'client_id' => API_KEY,
        'client_secret' => API_SECRET,
        'code' => $_GET['code'],
        'redirect_uri' => REDIRECT_URI,
    );
     
    $url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params);
     
    $context = stream_context_create(
        array('http' => 
            array('method' => 'POST',
            )
        )
    );
 
    $response = file_get_contents($url, false, $context);
 
    $token = json_decode($response);
 
    $_SESSION['access_token'] = $token->access_token; // guard this! 
    $_SESSION['expires_in']   = $token->expires_in; // relative time (in seconds)
    $_SESSION['expires_at']   = time() + $_SESSION['expires_in']; // absolute time
     
    return true;
}
 
function fetch($method, $resource, $body = '') {
 
    $opts = array(
        'http'=>array(
            'method' => $method,
            'header' => "Authorization: Bearer " . $_SESSION['access_token'] . "\r\n" . "x-li-format: json\r\n"
        )
    );
 
    $url = 'https://api.linkedin.com' . $resource;
 
    if (isset($params))
    {
    	if (count($params)) { $url .= '?' . http_build_query($params); }
 	}
    $context = stream_context_create($opts);
 
    $response = file_get_contents($url, false, $context);

    return json_decode($response);
}
?>
